package cn.lzm.serializer;

import cn.lizemin.constant.SerializerKeys;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lzm
 * @Date 2024/7/28
 * @description 使用工厂 + 单例模式来获取单例的序列化器
 */
public class SerializerFactory {

    private static final Map<String, Serializer> KEY_SERIALIZER_MAP = new HashMap<>();

    /**
     * 默认的序列化器
     */
    public static final Serializer DEFAULT_SERIALIZER = getSerializer(SerializerKeys.JDK);

    static {
        KEY_SERIALIZER_MAP.put(SerializerKeys.JDK, new JDKSerializer());
        KEY_SERIALIZER_MAP.put(SerializerKeys.HESSIAN, new HessianSerializer());
        KEY_SERIALIZER_MAP.put(SerializerKeys.KRYO, new KryoSerializer());
        KEY_SERIALIZER_MAP.put(SerializerKeys.JSON, new JsonSerializer());
    }

    public static Serializer getSerializer(String key) {
        return KEY_SERIALIZER_MAP.get(key);
    }


}
